package com.AnywheeBt;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;

public class KmlParse {		
	private FileOutputStream outStream;
	private static final String strDefaultXml="/system/anywhee_default.xml";
	
	private String strXmpPath=strDefaultXml;
	public KmlParse(String path) {
		if(path!=null && path!="" ) strXmpPath=path;
	}
	
	private List<String> getXmlKey(String keyname) {
		List<String> listKeyXml = new ArrayList<String>();		
		String strKey=keyname;
		if(strKey.length()==0)  return listKeyXml;
		for(int i=0;i<10;i++ )  {
			int npos=strKey.indexOf('/');			
			if( npos <=0 ) break;		
			String keyName=strKey.substring(0,npos);			
			listKeyXml.add(keyName);
			if((npos+1)>=strKey.length() ) break; 
			else	strKey=strKey.substring(npos+1);
		}
		return listKeyXml;
	}
	
	private InputStream getInputStream(String path) {
		 InputStream xmlis=null;
		 File Filexml = new File(path);
		 if( Filexml.exists() ) {		  
			  try{				 
				  xmlis = new FileInputStream(Filexml);				  
			  } catch (Exception e) {
				  xmlis=null;
			  }
		  }
		 return xmlis;
	}
	
	public BufferedWriter getBufferedWriterm(String path) {		
		File xmlFile = new File(path); 
		BufferedWriter writer=null;
		try {
			outStream = new FileOutputStream(xmlFile); 
			OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream, "UTF-8"); 
			writer = new BufferedWriter(outStreamWriter);
		}catch (Exception e) {   
	    	e.printStackTrace();   
	    }
		return writer;
	}	

	public String readKmlName(String keyname) {	  	   
	   List<String>  listKeyName=getXmlKey(keyname);	   
	   InputStream inStream=getInputStream(strXmpPath);
	   if(listKeyName==null || inStream==null ) return "";
	   XmlPullParser parser = Xml.newPullParser();	   
	   int nPos=0;
	   try { 
			parser.setInput(inStream, "UTF-8"); 
			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) { 
				switch (eventType) { 
					case XmlPullParser.START_DOCUMENT:						
						break; 
					case XmlPullParser.START_TAG:
						String name = parser.getName(); 
						if( nPos<listKeyName.size() ) {
							String strKey =listKeyName.get(nPos);							
							if( name.equalsIgnoreCase(strKey) ) {
								nPos++;
							}
							if(nPos==listKeyName.size() ) {
								name=parser.nextText();
								inStream.close();
								return name;								
							}
						}						
						break; 
					case XmlPullParser.END_TAG:
						break; 
				} 
				eventType = parser.next(); 
			} 
			inStream.close(); 			
	   } catch (Exception e) { 
			e.printStackTrace(); 
		} 	
	   return "";
	}
	
	public void writeKmlName(String keyname,String Name) {	
	     List<String>  listKeyName=getXmlKey(keyname);		
		 BufferedWriter writer = getBufferedWriterm(strXmpPath); 
		 if(listKeyName==null || writer==null) return;
		 XmlSerializer serializer = Xml.newSerializer();
		 int i=0;
		 try {
		     serializer.setOutput(writer); 
		     serializer.startDocument("UTF-8", true);		     
		     for(i=0;i< listKeyName.size();i++) {
		    	 String name=listKeyName.get(i);
		    	 serializer.startTag("", name); 
		     }
	         serializer.text(Name);
		     for(i=(listKeyName.size()-1); i>0;i--) {
		    	 String name=listKeyName.get(i);
		    	 serializer.endTag("", name); 
		     }	        
	         serializer.endDocument(); 
	         writer.toString();
			 writer.flush(); 
			 writer.close(); 
		  } catch (Exception e) { 
			 e.printStackTrace(); 
		  } 	 
		  return ;
	}	
}